import requests
import json
from concurrent.futures import ThreadPoolExecutor
import time
import random


print(int(round(time.time() * 1000)))


def public_request(timestamp, num):
    # print(int(round(time.time() * 1000)))
    # url = "http://10.8.0.22:8899/warn/report"
    url2 = "http://report.triumen.cn/add/timelog"
    url1 = "http://192.168.0.2:8899/add/timelog"
    r_url = ['http://www.google.com', 'http://www.feizhu.com', 'http://www.baidu.com', 'http://www.chaomeng.com',
             'http://www.alipay.com', 'http://www.douban.com', 'http://www.ebay.com', 'http://www.huya.com',
             'http://www.ins.com', 'http://www.json.com', 'http://www.kakao.com', 'http://www.list.com']
    dev_model = ['iPhone5s', 'HUAWEI', 'xiaomi']
    res_status_code = [404, 200, 500, 101, 403]
    app_name = ['upin', 'jinfu', 'retail']
    app_id = ['10000002', '10000005', '10000007']
    app_version = ['1.0.0', '1.1.0', '1.2.0']
    req_method = ["GET", "POST"]
    payload = {
        "app_name": random.choice(app_name),
        "app_id": random.choice(app_id),
        "app_desc": "我是描述",
        "app_version": random.choice(app_version),
        "req_id": "1",
        "req_p_id": "2",
        "req_session_id": "3",
        "req_pv_id": "4",
        "url": random.choice(r_url),
        "method": random.choice(req_method),
        "req_header": "application/json",
        "req_body ": "req_body ",
        "req_start_time": timestamp,
        "req_duration": random.randint(10, 100),
        "req_func": "5",
        "status_code": random.choice(res_status_code),
        "res_custom_code": random.choice(res_status_code),
        "res_header": "application/json",
        "res_body": {
            "account": "123456",
            "password": "12345678"
        },
        "res_biz_msg": "错误的提交参数",
        "res_biz_code": 4001,
        "dev_model": random.choice(dev_model),
        "dev_id": "E44EBC68-F68B-4D46-B1D1-64B7A9F0F2FC",
        "dev_sys_type": "iOS",
        "dev_sys_version": "12.4.6",
        "dev_platform_p_type": "APP",
        "dev_platform_type": "APP-IOS",
        "dev_platform_version": "1.1.0",
        "net_type": "4G"
    }
    headers = {
        'Content-Type': 'application/json'
    }
    response = requests.post(url1, headers=headers, data=json.dumps(payload))
    print(f'第{num + 1}次请求响应码: {response.status_code}')
    # result = response.json()
    # print(json.dumps(result, indent=2, ensure_ascii=False))


# for i in range(100):
#     public_request(int(round(time.time() * 1000)), i)
#     time.sleep(1)

# with ThreadPoolExecutor(max_workers=8) as t:
#     Threads = []
#     for i in range(100):
#         obj = t.submit(public_request())
#         Threads.append(obj)
